﻿using PLC.Core.Entities;
using PLC.Interface.BaseUserPermissions;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YWM.Application;
using YWM.Application.Linq.Extensions;
using YWM.Common.Extensions;

namespace PLC.Application.BaseUserPermissions.Services
{
    internal class BaseUserPermissionService : AsyncCrudService<BaseUserPermission, BaseUserPermissionDto, long, BaseUserPermissionResultRequestDto>, IBaseUserPermissionService
    {
        protected override IQueryable<BaseUserPermission> CreateFilteredQuery(BaseUserPermissionResultRequestDto input)
        {
            IQueryable<BaseUserPermission> queryable = base.CreateFilteredQuery(input)
                .WhereIf(!input.Account.IsNullOrWhiteSpace(), bup => bup.Account.Equals(input.Account));
            return queryable;
        }

        public async Task Delete(string account)
        {
            List<BaseUserPermission> userPermissions = await Repository.GetAllListAsync(bup => bup.Account.Equals(account));
            foreach (BaseUserPermission item in userPermissions)
            {
                await Repository.DeleteAsync(item);
            }
        }
    }
}
